package javolution.text;

import java.io.IOException;
import java.io.Writer;
import javax.realtime.MemoryArea;
import javolution.JavolutionError;
import javolution.context.ObjectFactory;
import javolution.io.UTF8StreamWriter;
import javolution.lang.MathLib;
import javolution.lang.Realtime;
import javolution.lang.ValueType;
import javolution.util.FastComparator;
import javolution.util.FastMap;
import javolution.xml.XMLSerializable;

/* loaded from: classes2.dex */
public final class Text implements CharSequence, Comparable, XMLSerializable, ValueType, Realtime {
    private static final int BLOCK_MASK = -32;
    private static final int BLOCK_SIZE = 32;
    private int _count;
    private final char[] _data;
    private Text _head;
    private Text _tail;
    private static final FastMap INTERN_INSTANCES = new FastMap().setKeyComparator(FastComparator.LEXICAL);
    private static final ThreadLocal TEXT_BUILDER = new ThreadLocal() { // from class: javolution.text.Text.1
        @Override // java.lang.ThreadLocal
        protected Object initialValue() {
            return new TextBuilder();
        }
    };
    public static final Text EMPTY = intern("");
    private static final Text TRUE = intern("true");
    private static final Text FALSE = intern("false");
    private static final UTF8StreamWriter SYSTEM_OUT_WRITER = new UTF8StreamWriter().setOutput(System.out);
    private static final ObjectFactory PRIMITIVE_FACTORY = new ObjectFactory() { // from class: javolution.text.Text.3
        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new Text(true);
        }
    };
    private static final ObjectFactory COMPOSITE_FACTORY = new ObjectFactory() { // from class: javolution.text.Text.4
        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new Text(false);
        }
    };

    public Text(String str) {
        this(str.length() <= 32);
        this._count = str.length();
        char[] cArr = this._data;
        if (cArr != null) {
            str.getChars(0, this._count, cArr, 0);
            return;
        }
        int i = ((this._count + 32) >> 1) & BLOCK_MASK;
        this._head = new Text(str.substring(0, i));
        this._tail = new Text(str.substring(i, this._count));
    }

    private Text(boolean z) {
        this._data = z ? new char[32] : null;
    }

    public static Text intern(CharSequence charSequence) {
        Text text = (Text) INTERN_INSTANCES.get(charSequence);
        return text != null ? text : internImpl(charSequence.toString());
    }

    public static Text intern(String str) {
        Text text = (Text) INTERN_INSTANCES.get(str);
        return text != null ? text : internImpl(str);
    }

    private static synchronized Text internImpl(final String str) {
        Text text;
        synchronized (Text.class) {
            if (!INTERN_INSTANCES.containsKey(str)) {
                MemoryArea.getMemoryArea(INTERN_INSTANCES).executeInArea(new Runnable() { // from class: javolution.text.Text.2
                    @Override // java.lang.Runnable
                    public void run() {
                        Text text2 = new Text(str);
                        Text.INTERN_INSTANCES.put(text2, text2);
                    }
                });
            }
            text = (Text) INTERN_INSTANCES.get(str);
        }
        return text;
    }

    private Text leftRotation() {
        Text text = this._tail;
        if (text._data != null) {
            return this;
        }
        Text text2 = text._head;
        return newComposite(newComposite(this._head, text2), text._tail);
    }

    private static Text newComposite(Text text, Text text2) {
        Text text3 = (Text) COMPOSITE_FACTORY.object();
        text3._count = text._count + text2._count;
        text3._head = text;
        text3._tail = text2;
        return text3;
    }

    private static Text newPrimitive(int i) {
        Text text = (Text) PRIMITIVE_FACTORY.object();
        text._count = i;
        return text;
    }

    private Text rightRotation() {
        Text text = this._head;
        return text._data != null ? this : newComposite(text._head, newComposite(text._tail, this._tail));
    }

    public static Text valueOf(char c) {
        Text newPrimitive = newPrimitive(1);
        newPrimitive._data[0] = c;
        return newPrimitive;
    }

    public static Text valueOf(char c, int i) {
        if (i >= 0) {
            if (i > 32) {
                int i2 = i >> 1;
                return newComposite(valueOf(c, i2), valueOf(c, i - i2));
            }
            Text newPrimitive = newPrimitive(i);
            for (int i3 = 0; i3 < i; i3++) {
                newPrimitive._data[i3] = c;
            }
            return newPrimitive;
        }
        throw new IndexOutOfBoundsException();
    }

    public static Text valueOf(double d) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(d);
        return textBuilder.toText();
    }

    public static Text valueOf(double d, int i, boolean z, boolean z2) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(d, i, z, z2);
        return textBuilder.toText();
    }

    public static Text valueOf(float f) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(f);
        return textBuilder.toText();
    }

    public static Text valueOf(int i) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(i);
        return textBuilder.toText();
    }

    public static Text valueOf(int i, int i2) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(i, i2);
        return textBuilder.toText();
    }

    public static Text valueOf(long j) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(j);
        return textBuilder.toText();
    }

    public static Text valueOf(long j, int i) {
        TextBuilder textBuilder = (TextBuilder) TEXT_BUILDER.get();
        textBuilder.clear().append(j, i);
        return textBuilder.toText();
    }

    public static Text valueOf(Object obj) {
        return obj instanceof Realtime ? ((Realtime) obj).toText() : obj instanceof Number ? valueOfNumber(obj) : valueOf(String.valueOf(obj));
    }

    private static Text valueOf(String str) {
        return valueOf(str, 0, str.length());
    }

    private static Text valueOf(String str, int i, int i2) {
        int i3 = i2 - i;
        if (i3 > 32) {
            int i4 = (((i3 + 32) >> 1) & BLOCK_MASK) + i;
            return newComposite(valueOf(str, i, i4), valueOf(str, i4, i2));
        }
        Text newPrimitive = newPrimitive(i3);
        str.getChars(i, i2, newPrimitive._data, 0);
        return newPrimitive;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static Text valueOf(TextBuilder textBuilder, int i, int i2) {
        int i3 = i2 - i;
        if (i3 > 32) {
            int i4 = (((i3 + 32) >> 1) & BLOCK_MASK) + i;
            return newComposite(valueOf(textBuilder, i, i4), valueOf(textBuilder, i4, i2));
        }
        Text newPrimitive = newPrimitive(i3);
        textBuilder.getChars(i, i2, newPrimitive._data, 0);
        return newPrimitive;
    }

    public static Text valueOf(boolean z) {
        return z ? TRUE : FALSE;
    }

    public static Text valueOf(char[] cArr) {
        return valueOf(cArr, 0, cArr.length);
    }

    public static Text valueOf(char[] cArr, int i, int i2) {
        if (i < 0 || i2 < 0 || i + i2 > cArr.length) {
            throw new IndexOutOfBoundsException();
        }
        if (i2 > 32) {
            int i3 = ((i2 + 32) >> 1) & BLOCK_MASK;
            return newComposite(valueOf(cArr, i, i3), valueOf(cArr, i + i3, i2 - i3));
        }
        Text newPrimitive = newPrimitive(i2);
        System.arraycopy(cArr, i, newPrimitive._data, 0, i2);
        return newPrimitive;
    }

    private static Text valueOfNumber(Object obj) {
        return obj instanceof Integer ? valueOf(((Integer) obj).intValue()) : obj instanceof Long ? valueOf(((Long) obj).longValue()) : obj instanceof Float ? valueOf(((Float) obj).floatValue()) : obj instanceof Double ? valueOf(((Double) obj).doubleValue()) : valueOf(String.valueOf(obj));
    }

    @Override // java.lang.CharSequence
    public char charAt(int i) {
        if (i < this._count) {
            char[] cArr = this._data;
            if (cArr != null) {
                return cArr[i];
            }
            Text text = this._head;
            int i2 = text._count;
            if (i >= i2) {
                text = this._tail;
                i -= i2;
            }
            return text.charAt(i);
        }
        throw new IndexOutOfBoundsException();
    }

    @Override // java.lang.Comparable
    public int compareTo(Object obj) {
        return FastComparator.LEXICAL.compare(this, obj);
    }

    public Text concat(Text text) {
        Text text2;
        int i = this._count;
        int i2 = text._count;
        int i3 = i + i2;
        if (i3 <= 32) {
            Text newPrimitive = newPrimitive(i3);
            getChars(0, this._count, newPrimitive._data, 0);
            text.getChars(0, text._count, newPrimitive._data, this._count);
            return newPrimitive;
        }
        if ((i << 1) < i2 && text._data == null) {
            if (text._head._count > text._tail._count) {
                text = text.rightRotation();
            }
            text2 = concat(text._head);
            text = text._tail;
        } else if ((text._count << 1) >= this._count || this._data != null) {
            text2 = this;
        } else {
            Text leftRotation = this._tail._count > this._head._count ? leftRotation() : this;
            text = leftRotation._tail.concat(text);
            text2 = leftRotation._head;
        }
        return newComposite(text2, text);
    }

    public boolean contentEquals(CharSequence charSequence) {
        if (charSequence.length() != this._count) {
            return false;
        }
        int i = 0;
        while (i < this._count) {
            int i2 = i + 1;
            if (charAt(i) != charSequence.charAt(i)) {
                return false;
            }
            i = i2;
        }
        return true;
    }

    public boolean contentEqualsIgnoreCase(CharSequence charSequence) {
        char upperCase;
        char upperCase2;
        if (this._count != charSequence.length()) {
            return false;
        }
        int i = 0;
        while (i < this._count) {
            char charAt = charAt(i);
            int i2 = i + 1;
            char charAt2 = charSequence.charAt(i);
            if (charAt != charAt2 && (upperCase = Character.toUpperCase(charAt)) != (upperCase2 = Character.toUpperCase(charAt2)) && Character.toLowerCase(upperCase) != Character.toLowerCase(upperCase2)) {
                return false;
            }
            i = i2;
        }
        return true;
    }

    @Override // javolution.lang.ValueType
    public Text copy() {
        if (this._data != null) {
            Text newPrimitive = newPrimitive(this._count);
            System.arraycopy(this._data, 0, newPrimitive._data, 0, this._count);
            return newPrimitive;
        }
        return newComposite(this._head.copy(), this._tail.copy());
    }

    public Text delete(int i, int i2) {
        if (i <= i2) {
            return subtext(0, i).concat(subtext(i2));
        }
        throw new IndexOutOfBoundsException();
    }

    public int depth() {
        if (this._data != null) {
            return 0;
        }
        return MathLib.max(this._head.depth(), this._tail.depth()) + 1;
    }

    public boolean endsWith(CharSequence charSequence) {
        return startsWith(charSequence, length() - charSequence.length());
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Text) {
            Text text = (Text) obj;
            if (this._count != text._count) {
                return false;
            }
            int i = 0;
            while (i < this._count) {
                int i2 = i + 1;
                if (charAt(i) != text.charAt(i)) {
                    return false;
                }
                i = i2;
            }
            return true;
        }
        return false;
    }

    public void getChars(int i, int i2, char[] cArr, int i3) {
        char[] cArr2 = this._data;
        if (cArr2 != null) {
            if (i < 0 || i2 > this._count || i > i2) {
                throw new IndexOutOfBoundsException();
            }
            System.arraycopy(cArr2, i, cArr, i3, i2 - i);
            return;
        }
        Text text = this._head;
        int i4 = text._count;
        if (i2 > i4) {
            if (i < i4) {
                text.getChars(i, i4, cArr, i3);
                this._tail.getChars(0, i2 - i4, cArr, (i3 + i4) - i);
                return;
            }
            text = this._tail;
            i -= i4;
            i2 -= i4;
        }
        text.getChars(i, i2, cArr, i3);
    }

    public int hashCode() {
        int length = length();
        int i = 0;
        for (int i2 = 0; i2 < length; i2++) {
            i = (i * 31) + charAt(i2);
        }
        return i;
    }

    public int indexOf(char c, int i) {
        int indexOf;
        if (this._data != null) {
            for (int max = MathLib.max(i, 0); max < this._count; max++) {
                if (this._data[max] == c) {
                    return max;
                }
            }
            return -1;
        }
        Text text = this._head;
        int i2 = text._count;
        if (i >= i2 || (indexOf = text.indexOf(c, i)) < 0) {
            int indexOf2 = this._tail.indexOf(c, i - i2);
            if (indexOf2 >= 0) {
                return indexOf2 + i2;
            }
            return -1;
        }
        return indexOf;
    }

    public int indexOf(CharSequence charSequence) {
        return indexOf(charSequence, 0);
    }

    public int indexOf(CharSequence charSequence, int i) {
        int length = charSequence.length();
        int max = Math.max(0, i);
        int i2 = this._count - length;
        if (length == 0) {
            if (max > i2) {
                return -1;
            }
            return max;
        }
        char charAt = charSequence.charAt(0);
        while (true) {
            int indexOf = indexOf(charAt, max);
            if (indexOf < 0 || indexOf > i2) {
                break;
            }
            boolean z = true;
            int i3 = 1;
            while (true) {
                if (i3 >= length) {
                    break;
                } else if (charAt(indexOf + i3) != charSequence.charAt(i3)) {
                    z = false;
                    break;
                } else {
                    i3++;
                }
            }
            if (z) {
                return indexOf;
            }
            max = indexOf + 1;
        }
        return -1;
    }

    public int indexOfAny(CharSet charSet) {
        return indexOfAny(charSet, 0, length());
    }

    public int indexOfAny(CharSet charSet, int i) {
        return indexOfAny(charSet, i, length() - i);
    }

    public int indexOfAny(CharSet charSet, int i, int i2) {
        int i3 = i2 + i;
        while (i < i3) {
            if (charSet.contains(charAt(i))) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public Text insert(int i, Text text) {
        return subtext(0, i).concat(text).concat(subtext(i));
    }

    public boolean isBlank() {
        return isBlank(0, length());
    }

    public boolean isBlank(int i, int i2) {
        while (i < i2) {
            if (charAt(i) > ' ') {
                return false;
            }
            i++;
        }
        return true;
    }

    public int lastIndexOf(char c, int i) {
        int lastIndexOf;
        if (this._data == null) {
            int i2 = this._head._count;
            return (i < i2 || (lastIndexOf = this._tail.lastIndexOf(c, i - i2)) < 0) ? this._head.lastIndexOf(c, i) : lastIndexOf + i2;
        }
        for (int min = MathLib.min(i, this._count - 1); min >= 0; min--) {
            if (this._data[min] == c) {
                return min;
            }
        }
        return -1;
    }

    public int lastIndexOf(CharSequence charSequence) {
        return lastIndexOf(charSequence, this._count);
    }

    public int lastIndexOf(CharSequence charSequence, int i) {
        int length = charSequence.length();
        int min = Math.min(i, this._count - length);
        if (length == 0) {
            if (min < 0) {
                return -1;
            }
            return min;
        }
        char charAt = charSequence.charAt(0);
        while (true) {
            int lastIndexOf = lastIndexOf(charAt, min);
            if (lastIndexOf < 0) {
                return -1;
            }
            boolean z = true;
            int i2 = 1;
            while (true) {
                if (i2 >= length) {
                    break;
                } else if (charAt(lastIndexOf + i2) != charSequence.charAt(i2)) {
                    z = false;
                    break;
                } else {
                    i2++;
                }
            }
            if (z) {
                return lastIndexOf;
            }
            min = lastIndexOf - 1;
        }
    }

    public int lastIndexOfAny(CharSet charSet) {
        return lastIndexOfAny(charSet, 0, length());
    }

    public int lastIndexOfAny(CharSet charSet, int i) {
        return lastIndexOfAny(charSet, i, length() - i);
    }

    public int lastIndexOfAny(CharSet charSet, int i, int i2) {
        int i3 = i2 + i;
        do {
            i3--;
            if (i3 < i) {
                return -1;
            }
        } while (!charSet.contains(charAt(i3)));
        return i3;
    }

    @Override // java.lang.CharSequence
    public int length() {
        return this._count;
    }

    public Text padLeft(int i) {
        return padLeft(i, ' ');
    }

    public Text padLeft(int i, char c) {
        return insert(0, valueOf(c, i <= length() ? 0 : i - length()));
    }

    public Text padRight(int i) {
        return padRight(i, ' ');
    }

    public Text padRight(int i, char c) {
        return concat(valueOf(c, i <= length() ? 0 : i - length()));
    }

    public Text plus(Object obj) {
        return concat(valueOf(obj));
    }

    public void print() {
        try {
            synchronized (SYSTEM_OUT_WRITER) {
                print(SYSTEM_OUT_WRITER);
                SYSTEM_OUT_WRITER.flush();
            }
        } catch (IOException e) {
            throw new JavolutionError(e);
        }
    }

    public void print(Writer writer) throws IOException {
        char[] cArr = this._data;
        if (cArr != null) {
            writer.write(cArr, 0, this._count);
            return;
        }
        this._head.print(writer);
        this._tail.print(writer);
    }

    public void println() {
        try {
            synchronized (SYSTEM_OUT_WRITER) {
                println(SYSTEM_OUT_WRITER);
                SYSTEM_OUT_WRITER.flush();
            }
        } catch (IOException e) {
            throw new JavolutionError(e);
        }
    }

    public void println(Writer writer) throws IOException {
        print(writer);
        writer.write(10);
    }

    public Text replace(CharSequence charSequence, CharSequence charSequence2) {
        int indexOf = indexOf(charSequence);
        return indexOf < 0 ? this : subtext(0, indexOf).concat(valueOf(charSequence2)).concat(subtext(indexOf + charSequence.length()).replace(charSequence, charSequence2));
    }

    public Text replace(CharSet charSet, CharSequence charSequence) {
        int indexOfAny = indexOfAny(charSet);
        return indexOfAny < 0 ? this : subtext(0, indexOfAny).concat(valueOf(charSequence)).concat(subtext(indexOfAny + 1).replace(charSet, charSequence));
    }

    public boolean startsWith(CharSequence charSequence) {
        return startsWith(charSequence, 0);
    }

    public boolean startsWith(CharSequence charSequence, int i) {
        int length = charSequence.length();
        if (i < 0 || i > length() - length) {
            return false;
        }
        int i2 = i;
        int i3 = 0;
        while (i3 < length) {
            int i4 = i3 + 1;
            int i5 = i2 + 1;
            if (charSequence.charAt(i3) != charAt(i2)) {
                return false;
            }
            i3 = i4;
            i2 = i5;
        }
        return true;
    }

    @Override // java.lang.CharSequence
    public CharSequence subSequence(int i, int i2) {
        return subtext(i, i2);
    }

    public Text subtext(int i) {
        return subtext(i, length());
    }

    public Text subtext(int i, int i2) {
        int i3;
        if (this._data == null) {
            Text text = this._head;
            int i4 = text._count;
            return i2 <= i4 ? text.subtext(i, i2) : i >= i4 ? this._tail.subtext(i - i4, i2 - i4) : (i == 0 && i2 == this._count) ? this : this._head.subtext(i, i4).concat(this._tail.subtext(0, i2 - i4));
        } else if (i < 0 || i > i2 || i2 > (i3 = this._count)) {
            throw new IndexOutOfBoundsException();
        } else {
            if (i == 0 && i2 == i3) {
                return this;
            }
            if (i == i2) {
                return EMPTY;
            }
            int i5 = i2 - i;
            Text newPrimitive = newPrimitive(i5);
            System.arraycopy(this._data, i, newPrimitive._data, 0, i5);
            return newPrimitive;
        }
    }

    public Text toLowerCase() {
        if (this._data == null) {
            return newComposite(this._head.toLowerCase(), this._tail.toLowerCase());
        }
        Text newPrimitive = newPrimitive(this._count);
        for (int i = 0; i < this._count; i++) {
            newPrimitive._data[i] = Character.toLowerCase(this._data[i]);
        }
        return newPrimitive;
    }

    @Override // java.lang.CharSequence
    public String toString() {
        char[] cArr = this._data;
        if (cArr != null) {
            return new String(cArr, 0, this._count);
        }
        int i = this._count;
        char[] cArr2 = new char[i];
        getChars(0, i, cArr2, 0);
        return new String(cArr2, 0, this._count);
    }

    @Override // javolution.lang.Realtime
    public Text toText() {
        return this;
    }

    public Text toUpperCase() {
        if (this._data == null) {
            return newComposite(this._head.toUpperCase(), this._tail.toUpperCase());
        }
        Text newPrimitive = newPrimitive(this._count);
        for (int i = 0; i < this._count; i++) {
            newPrimitive._data[i] = Character.toUpperCase(this._data[i]);
        }
        return newPrimitive;
    }

    public Text trim() {
        int length = length() - 1;
        int i = 0;
        while (i <= length && charAt(i) <= ' ') {
            i++;
        }
        while (length >= i && charAt(length) <= ' ') {
            length--;
        }
        return subtext(i, length + 1);
    }

    public Text trimEnd() {
        int length = length() - 1;
        while (length >= 0 && charAt(length) <= ' ') {
            length--;
        }
        return subtext(0, length + 1);
    }

    public Text trimStart() {
        int length = length() - 1;
        int i = 0;
        while (i <= length && charAt(i) <= ' ') {
            i++;
        }
        return subtext(i, length + 1);
    }
}
